package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetDistributionConfigReq;
import com.shop2cn.iapi.sqdistributor.parameter.response.GetDistributionConfigResponse;
import com.shop2cn.iapi.sqdistributor.parameter.response.pojo.DistributorLevelCommissionConfigVo;
import com.shop2cn.iapi.sqdistributor.service.GetDistributionConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.CommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionConfig;
import com.ymttest.database.sqlwapper.sqdistributor.CommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionConfigWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetDistributionConfig {
	
	//java接口
	private static GetDistributionConfigReq getDistributionConfigReq;
	private static GetDistributionConfigCall getDistributionConfigCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商户的分销员配置");
	}

	@Before
	public void caseUp() {
		getDistributionConfigReq = new GetDistributionConfigReq();
		getDistributionConfigCall = new GetDistributionConfigCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getDistributionConfigCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {

		DistributionConfigWrapper distributionConfigWrapper = new DistributionConfigWrapper();

		DistributionConfig actualResult = distributionConfigWrapper
				.selectByMerchantId(getDistributionConfigReq.getMerchantId());

		GetDistributionConfigResponse expectResult = getDistributionConfigCall
				.getDistributionConfigResponse();

		if (null == actualResult) {

			Logger.comment("默认配置校验");

			Logger.verifyEquals(expectResult.getMerchantId(),
					getDistributionConfigReq.getMerchantId(), "验证Merchant_id");
			Logger.verifyEquals(expectResult.getUserDistributorBindRuleType(),
					0, "验证User_distributor_bind_rule_type");
			Logger.verifyEquals(expectResult.isSelfPurchaseCommission(), false,
					"验证Self_purchase_commission");
			Logger.verifyEquals(expectResult.getDistributionLayers(), 1,
					"验证Distribution_layers");
			Logger.verifyEquals(
					expectResult.getDistributionSettlementDelayDays(), 0,
					"验证Distribution_settlement_delay_days");
			Logger.verifyEquals(expectResult.getBindingEffectiveThreshold(), 0,
					"验证BindingEffectiveThreshold");

			List<DistributorLevelCommissionConfigVo> expectResultList = expectResult
					.getDistributorLevelCommissionConfigVoList();

			Logger.verifyEquals(expectResultList.size(), 1,
					"验证CommisionRate的size");

			for (int i = 0; i < expectResultList.size(); i++) {

				DistributorLevelCommissionConfigVo expectDistributorLevelCommissionConfigVoResult = expectResultList
						.get(i);

				Logger.verifyEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getDistributionConfigId(), 0L,
						"验证Distribution_config_id");
				Logger.verifyEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getDistributorLevelIndex(), 0,
						"验证Distributor_level_index");
				Logger.verifyEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getDistributorLevelLabel(), "普通分销员",
						"验证Distributor_level_label");
				DataUtil.verifyDecimalEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getLayer1Rate(), null, "验证Layer1_rate");
				DataUtil.verifyDecimalEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getLayer2Rate(), null, "验证Layer2_rate");
				DataUtil.verifyDecimalEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getLayer3Rate(), null, "验证Layer3_rate");
			}
		} else {

			Logger.verifyEquals(expectResult.getMerchantId(),
					actualResult.getMerchant_id(), "验证Merchant_id");
			Logger.verifyEquals(expectResult.getUserDistributorBindRuleType(),
					actualResult.getUser_distributor_bind_rule_type(),
					"验证User_distributor_bind_rule_type");
			Logger.verifyEquals(expectResult.isSelfPurchaseCommission(),
					actualResult.getSelf_purchase_commission(),
					"验证Self_purchase_commission");
			Logger.verifyEquals(expectResult.getDistributionLayers(),
					actualResult.getDistribution_layers(),
					"验证Distribution_layers");
			Logger.verifyEquals(
					expectResult.getDistributionSettlementDelayDays(),
					actualResult.getDistribution_settlement_delay_days(),
					"验证Distribution_settlement_delay_days");
			Logger.verifyEquals(expectResult.getBindingEffectiveThreshold(),
					actualResult.getBinding_effective_threshold(),
					"验证BindingEffectiveThreshold");

			List<DistributorLevelCommissionConfigVo> expectResultList = expectResult
					.getDistributorLevelCommissionConfigVoList();

			CommisionRateWrapper commisionRateWrapper = new CommisionRateWrapper();
			List<CommisionRate> actualList = commisionRateWrapper
					.selectByConfigId(actualResult.getId());

			Logger.verifyEquals(expectResultList.size(), actualList.size(),
					"验证CommisionRate的size");

			for (int i = 0; i < actualList.size(); i++) {

				CommisionRate actualCommisionRateResult = actualList.get(i);
				DistributorLevelCommissionConfigVo expectDistributorLevelCommissionConfigVoResult = expectResultList
						.get(i);

				Logger.verifyEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getDistributionConfigId(),
						actualCommisionRateResult.getDistribution_config_id(),
						"验证Distribution_config_id");
				Logger.verifyEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getDistributorLevelIndex(),
						actualCommisionRateResult.getDistributor_level_index(),
						"验证Distributor_level_index");
				Logger.verifyEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getDistributorLevelLabel(),
						actualCommisionRateResult.getDistributor_level_label(),
						"验证Distributor_level_label");
				DataUtil.verifyDecimalEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getLayer1Rate(), actualCommisionRateResult
								.getLayer1_rate(), "验证Layer1_rate");
				DataUtil.verifyDecimalEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getLayer2Rate(), actualCommisionRateResult
								.getLayer2_rate(), "验证Layer2_rate");
				DataUtil.verifyDecimalEquals(
						expectDistributorLevelCommissionConfigVoResult
								.getLayer3Rate(), actualCommisionRateResult
								.getLayer3_rate(), "验证Layer3_rate");
			}
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributionConfig_001() {
		Logger.start(true, "商户的分销员配置，获取成功");
		
		try {
			
			getDistributionConfigReq.setMerchantId(500861049L);
			
			getDistributionConfigCall.setData(getDistributionConfigReq);
			getDistributionConfigCall.callService();
			
			Logger.verifyEquals(true, getDistributionConfigCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributionConfig_002() {
		Logger.start(true, "获取未设置分销员配置，返回默认配置");
		
		try {
			
			getDistributionConfigReq.setMerchantId(50086L);
			
			getDistributionConfigCall.setData(getDistributionConfigReq);
			getDistributionConfigCall.callService();
			
			Logger.verifyEquals(true, getDistributionConfigCall.getSuccess(), "验证success");

			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
